/**
 *
 */
package com.wedroid.r2d2.support.response;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.wedroid.r2d2.support.sync.SyncAction;
import com.wedroid.r2d2.support.sync.SyncSelector;

/**
 * @author lute
 *
 */
public class SyncCheckResponse implements DroidResponse {

    private static final String REGEX = "window.synccheck=\\{retcode:\"(\\d+)\",selector:\"(\\d+)\"}";

    private SyncAction action;

    private SyncSelector selector;

    private Pattern pattern;

    /**
     *
     */
    public SyncCheckResponse() {
        this.pattern = Pattern.compile(REGEX);
    }

    /* (non-Javadoc)
     * @see com.wedroid.r2d2.support.response.DroidResponse#parse(java.lang.String)
     */
    public void parse(String response) {
        Matcher matcher = pattern.matcher(response);
        if (matcher.find()) {
            this.action = SyncAction.retCodeOf(Integer.valueOf(matcher.group(1)));
            this.selector = SyncSelector.selectorOf(Integer.valueOf(matcher.group(2)));
        }
    }

    /* (non-Javadoc)
     * @see com.wedroid.r2d2.support.response.DroidResponse#isSuccess()
     */
    public boolean isSuccess() {
        return this.action != null && this.selector != null;
    }

    /**
     * @return the action
     */
    public SyncAction getAction() {
        return action;
    }

    /**
     * @return the selector
     */
    public SyncSelector getSelector() {
        return selector;
    }

}
